#!/bin/sh

#########################################
# checkout: checkout source code into path
#
# usage:
#   checkout git_repo localpath
#########################################
checkout() {
  if [ ! -d $2 ]; then
    git clone $1 $2
  fi

  pushd .
  cd $2
  # remove any local unstaged changes
  git clean -f
  git reset --hard
  # switch to online branch
  git checkout master
  if [ `git branch --list online` ]
  then
    git branch -D online
  fi
  git checkout -B online
  # pull lastest change from master or tag
  git pull origin $3
  popd
}

#########################################
# build_package: build package into ansible path
#
# usage:
#   build_package packagename localpath
#########################################
build_package() {
  #cp ./bin/deploy_bootstrap.sh $2/.
  #./bin/makeself.sh $2 deploy-`date "+%Y%m%d%H%M%S"`.run "deploy installer" ./deploy_bootstrap.sh

  pushd .
  cd $2/..
  tar --exclude=.git* --exclude=*.tar.gz -czf $1.tar.gz $1
  popd
  mkdir -p roles/$1/files
  /bin/cp -f $2/../$1.tar.gz roles/$1/files
}

#########################################
# rsync_package: rsync package to remote hosts
#
# usage:
#   rsync_package username remotehost remotepath localpath ssh_port
#########################################
rsync_package() {
  rsync -e "ssh -p $5" -avz --progress --delete --exclude ".git*" --exclude "pckg/" --exclude "*.pyc" $4 $1@$2:$3
}

#########################################
# install_package: install package with ansible
#
# usage:
#   install_package packagename sshkey inventoryfile
#########################################
install_package() {
  TAG_PARA=
  LIMIT_PARA=
  if [ ! -z "$4" ]; then
    TAG_PARA="--tags $4"
  fi
  if [ ! -z "$5" ]; then
    LIMIT_PARA="--limit $5"
  fi
  ANSIBLE_MODULE_LANG=en_US.UTF-8 ansible-playbook -vvv --private-key=$2 -i $3 $TAG_PARA $LIMIT_PARA $1.yml
}

#########################################
# install_package: install package with ansible
#
# usage:
#   install_package_manual ssh_port ssh_user host_ip local_path package_name
#########################################
install_tizi_manual() {
  ssh -p $1 $2@$3 -t "sudo su -c 'if [ $# -ge 6 -a \"$6\"=\"production\" ];then\
    /bin/cp /home/$2/$5/online_index.php /home/$2/$5/index.php;\
  else\
    /bin/cp /home/$2/$5/testing_index.php /home/$2/$5/index.php;\
  fi;\
  cd /home/$2/$5/application/views/static/;\
  ln -s js.$7 js;\
  ln -s css.$7 css;\
  mkdir -p /home/$2/$5/application/templates_c;\
  mkdir /home/$2/$5/application/logs/;\
  mkdir -m 0777 /home/$2/$5/application/third_party/log;\
  /bin/rm -f /space1/$5/application/templates_c/*;\
  rsync -avz --progress --delete --exclude=\"*.pyc\" /home/$2/$5 /space1/;\
  chown -R www-data:www-data /space1/$5;\
  /bin/rm -f /space1/$5/application/templates_c/*;\
  /etc/init.d/nginx reload'"
}

#########################################
# install_package: install package with ansible
#
# usage:
#   install_waijiao_manual ssh_port ssh_user host_ip local_path package_name
#########################################
install_waijiao_manual() {
  ssh -p $1 $2@$3 -t "sudo su -c 'rsync -avz --progress --delete /home/$2/$5 /space1/;/bin/cp /space1/$5/webroot/online_index.php /space1/$5/webroot/index.php;chown -R www-data:www-data /space1/$5;/etc/init.d/nginx reload'"
}

install_tizilib_manual() {
  ssh -p $1 $2@$3 -t "sudo su -c 'cd /home/$2/$5/library/views/static/;/bin/rm -rf lib;/bin/ln -s lib.$6 lib;rsync -avz --progress --delete /home/$2/$5 /space1/;/etc/init.d/nginx reload'"
}

install_tizi_child_manual() {
  ssh -p $1 $2@$3 -t "sudo su -c '/bin/cp /home/$2/$5/online_index.php /home/$2/$5/index.php;rsync -avz --progress --delete /home/$2/$5 /space1/;chown -R www-data:www-data /space1/$5;/etc/init.d/nginx reload'"
}

install_itizi_manual() {
  ssh -p $1 $2@$3 -t "sudo su -c 'mkdir -p /home/$2/$5/application/templates_c;mkdir -p /home/$2/$5/application/logs;ln -s /space1/tizi_lib/library/views/static/lib /home/$2/$5/application/views/static/$6/lib;/bin/cp /home/$2/$5/online_index.php /home/$2/$5/index.php;rsync -avz --progress --delete /home/$2/$5 /space1/;chown -R www-data:www-data /space1/$5;/etc/init.d/nginx reload'"
}

#########################################
# compress: use yuicompressor to compress all js and css files
#
# usage:
#   compress zujuan_teacher_git_root_path
#########################################
compress_cssjs() {
  if [ $# -gt 0 ] && [ -d $1 ];then
    for file in `find $1 -name "*.js"`
      do
	java -jar ./bin/yuicompressor-2.4.8.jar $file > $file.yui
        mv $file.yui $file
      done

    for file in `find $1 -name "*.css"`
      do
        java -jar ./bin/yuicompressor-2.4.8.jar $file > $file.yui
        mv $file.yui $file
    done
  else
    echo 'Usage: compress [zujuan_teacher_path]'
  fi
}
